import { Module } from 'vuex';
import { userState } from './types';
import { RootState } from '../../type';
import api from '@/api'; // api模块
import { getAllOptions } from '@/utils/dic';
import storage from '@/utils/helper/storage';
const state: userState = {
    token: storage.get('token') || '',
    isWarningChangePwd: !!storage.get('isWarningChangePwd') || false, // 是否已经提示过用户修改密码
    info: {},
    authInfo: [],
    dicOptions: {},
    searchHistoryRecord: [],
    moveData: {}
};

type StateType = typeof state

const user: Module<StateType, RootState> = {
    namespaced: true,
    state,
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
            if (token) {
                storage.set('token', token);
            } else {
                storage.remove('token');
            }
        },
        SET_ISWARNINGCHANGEPWD: (state, res) => {
            state.isWarningChangePwd = res;
            if (res) {
                storage.set('isWarningChangePwd', res);
            } else {
                storage.remove('isWarningChangePwd');
            }
        },
        SET_INFO: (state, info) => {
            state.info = info;
        },
        SET_AUTH: (state, info) => {
            // console.log('所有按钮权限', info)
            state.authInfo = info;
        },
        SET_DICOPTIONS: (state, payload) => {
            state.dicOptions = payload;
        },
        SET_HISTORYRECORD: (state, payload) => {
            state.searchHistoryRecord = payload;
        },
        SET_MOVEDATA: (state, payload) => {
            state.moveData = payload;
        }
    },
    actions: {
        async Login({ commit }, userInfo) {
            try {
                const res = await api.userModel.login(userInfo);
                // const res = await api.userModel.loginCheckInside(userInfo)
                if (res.success) {
                    commit('SET_TOKEN', res.moule);
                }
                return res;
            } catch (err) {
                return err;
            }
        },
        async GetUserInfo({ commit }) {
            try {
                const res = await api.userModel.getCurrentUser();
                if (res.success) {
                    const isPM = res.moule.roles.some((item) => ['产品经理'].includes(item));
                    const { userInfoDTO } = res.moule;
                    if (userInfoDTO) {
                        const { userId, name, orgName } = userInfoDTO;
                        const orgNameArr = orgName ? orgName.split('-') : [];
                        res.moule.employeeDTO = {
                            ...userInfoDTO,
                            empId: userId,
                            empName: name,
                            orgList: [
                                { name: orgNameArr.length ? orgNameArr[orgNameArr.length - 1] : '' }
                            ]
                        };
                    }
                    if (res.moule.employeeDTO) {
                        res.moule.employeeDTO.isPM = isPM;
                    }
                    // 是否显示左侧筛选树
                    const isShowSideTree = res.moule.pmpRoles.some((item) => ['admin', '观察者', '领导', '产品管理委员会'].includes(item));
                    if (res.moule.employeeDTO) {
                        res.moule.employeeDTO.isShowSideTree = isShowSideTree;
                        // res.moule.employeeDTO.isShowSideTree = true
                    }
                    commit('SET_INFO', res.moule);
                }
                return res;
            } catch (err) {
                return err;
            }
        },
        async GetDicOptions({ commit }) {
            try {
                const res = await getAllOptions();
                if (res.success) {
                    commit('SET_DICOPTIONS', res.moule);
                }
                return res;
            } catch (err) {
                return err;
            }
        },
        // 登出
        Logout({ commit }) {
            commit('SET_TOKEN', '');
            commit('SET_ISWARNINGCHANGEPWD', false);
            location.reload();
        },
        // 获取用户搜索历史记录
        async GetSearchHistoryRecord({ commit }) {
            const query = {
                tenantId: 'teep-pmp'
            };
            const { success, info } = await api.queryHistoryModel.getQueryHistories(query);
            if (success) {
                commit('SET_HISTORYRECORD', info);
            } else {
                commit('SET_HISTORYRECORD', []);
            }
        },
        // 删除所有历史记录
        async DeleteAllSearchHistoryRecord({ commit }) {
            const query = {
                tenantId: 'teep-pmp'
            };
            const { success } = await api.queryHistoryModel.deleteByEmpId(query);
            if (success) {
                commit('SET_HISTORYRECORD', []);
            } else {
                commit('SET_HISTORYRECORD', []);
            }
        }
    }
};

export default user;
